#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
Created on 2014年9月5日

@author: monstar-216
'''

from random import randint


def match(user_lv, lv_list_status, ride_rounds ,lv_gap):
    print 'user_lv, lv_list_status, ride_rounds ,lv_gap',user_lv, lv_list_status, ride_rounds ,lv_gap
    print '------------ride_rounds,',ride_rounds
    min_lv = lv_list_status[0]['lv']
    max_lv = lv_list_status[-1]['lv']
    lv_list = []
    t = 0
    for i in range(0,max_lv+1):
        if lv_list_status[t]['lv'] != i:
            lv_list.append(0)
        else:
            lv_list.append(lv_list_status[t]['count'])
            t += 1
    lv_list[user_lv] -= 1 #校正用户当前等级的count
    print lv_list
    low_lv = min_lv if user_lv - lv_gap <= min_lv else user_lv - lv_gap
    up_lv = max_lv if user_lv + lv_gap >= max_lv else user_lv + lv_gap
    res_list = []
    big_lv = small_lv = equal_lv = None
    print '@@@@@@@@@low_lv,up_lv',low_lv,up_lv
    
    #for big_area
    t_count = 0
    t_lv = up_lv
    while t_count < 6:
        if t_lv == low_lv: break
        t_count += lv_list[t_lv]
        t_lv -= 1
    if up_lv - t_lv > 3:
        lv_area = lv_list[t_lv:up_lv+1]
        lv_start = t_lv
    else:
        lv_area = lv_list[up_lv-3:up_lv+1]
        lv_start = up_lv-3
    i = 0
    while i <= 13:
        big_lv = get_lv_limit_by_area(lv_area, lv_start, ride_rounds+i, 1)
        if big_lv not in res_list:
            res_list.append(big_lv)
            break
        i += 1
    #for small_area
    print '----for small_area'
    t_count = 0
    t_lv = low_lv
    while t_count < 6:
        if t_lv == up_lv: break
        t_count += lv_list[t_lv]
        t_lv += 1
    if t_lv - low_lv > 3:
        lv_area = lv_list[low_lv:t_lv]
        lv_start = low_lv
    else:
        lv_area = lv_list[low_lv:low_lv+4]
        lv_start = low_lv
    i = 0
    while i <= 13:
        small_lv = get_lv_limit_by_area(lv_area, lv_start, ride_rounds+i, -1)
        if small_lv not in res_list:
            res_list.append(small_lv)
            break
        i += 1
        
    #for equal area
    print '----for equal'
    if user_lv==low_lv:
        eq_status = -1
        t_count = 0
        t_lv = low_lv
        while t_count < 6:
            if t_lv == up_lv: break
            t_count += lv_list[t_lv]
            t_lv += 1
        if t_lv - low_lv > 3:
            lv_area = lv_list[low_lv:t_lv]
            lv_start = low_lv
        else:
            lv_area = lv_list[low_lv:low_lv+4]
            lv_start = low_lv
        i = 0
        while i <= 13:
            equal_lv = get_lv_limit_by_area(lv_area, lv_start, ride_rounds+i, 1)
            if equal_lv not in res_list:
                res_list.append(equal_lv)
                break
            i += 1
    elif user_lv == up_lv:
        eq_status = 1
        t_count = 0
        t_lv = up_lv
        while t_count < 6:
            if t_lv == low_lv: break
            t_count += lv_list[t_lv]
            t_lv -= 1
        if up_lv - t_lv > 3:
            lv_area = lv_list[t_lv:up_lv+1]
            lv_start = t_lv
        else:
            lv_area = lv_list[up_lv-3:up_lv+1]
            lv_start = up_lv-3
        i = 0
        while i <= 13:
            equal_lv = get_lv_limit_by_area(lv_area, lv_start, ride_rounds+i, -1)
            if equal_lv not in res_list:
                res_list.append(equal_lv)
                break
            i+=1
    else:
        if lv_list[user_lv] <= 3:
            lv_area = lv_list[user_lv-1:user_lv+2]
            lv_start = user_lv - 1
            p = 0 if ride_rounds % 2 == 0 else -1
            eq_status = 1 if ride_rounds % 2 == 0 else -1
            i = 0
            while i <= 13:
                equal_lv = get_lv_limit_by_area(lv_area, lv_start, ride_rounds+i, p)
                if equal_lv not in res_list:
                    res_list.append(equal_lv)
                    break
                i += 1
        else:
            eq_status = 0
            lv_area = lv_list[user_lv:user_lv+1]
            lv_start = user_lv
            i = 0
            while i <= 13:
                equal_lv = get_lv_limit_by_area(lv_area, lv_start, ride_rounds+i, 1)
                if equal_lv not in res_list:
                    res_list.append(equal_lv)
                    break
                i += 1
    
    return big_lv,small_lv,equal_lv,eq_status

def get_lv_limit_by_area(lv_area, lv_start, ride_rounds, priority):
    print '------------lv_area,lv_start,ride_rounds,priority',lv_area,lv_start,ride_rounds,priority
    search_area = lv_area if priority < 0 else lv_area[::-1]
    area_count = sum(search_area)
    res = None
    if area_count > 0:
        res = {}
        t = 0
        if ride_rounds >= area_count:
            limit = ride_rounds % area_count 
        else:
            limit = ride_rounds
        for i,v in enumerate(search_area):
            print search_area
            t += v
            print 'limit,i,v,t',limit,i,v,t
            if t >= limit:
                res['lv'] = lv_start+i if priority < 0 else lv_start+len(lv_area)-i
                res['limit'] = limit-(t-v)
                break
    return res

tlv_list =  [{'lv': 3L, 'count': 55L}, {'lv': 4L, 'count': 7L}, {'lv': 5L, 'count': 2L}, {'lv': 6L, 'count': 5L}, {'lv': 7L, 'count': 4L}, {'lv': 8L, 'count': 1L}, {'lv': 9L, 'count': 4L}, {'lv': 10L, 'count': 2L}, {'lv': 15L, 'count': 2L}, {'lv': 16L, 'count': 1L}, {'lv': 17L, 'count': 1L}]
if __name__ == '__main__':
    print match(3,tlv_list,4,10)
